package com.jsgt.greekfire.ui.mine.coindetail.recharge

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.graphics.Bitmap
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.QRCodeUtils
import com.fhx.core.utils.ScreenUtil
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.RechargeAddress
import com.jsgt.greekfire.factory.CoinViewModelFactory
import com.jsgt.greekfire.ui.mine.coindetail.recharge.rechargerecord.RechargeRecordActivity
import com.jsgt.greekfire.viewmodel.CoinViewModel
import kotlinx.android.synthetic.main.activity_recharge.*


/**
 * 充币
 * author fhx
 * create time 2020/4/24 12:23
 **/
class RechargeActivity : BaseActivity() {
    var rechargeAddress: RechargeAddress? = null
    private val coinViewModel: CoinViewModel by lazy {
        ViewModelProvider(
            this,
            CoinViewModelFactory()
        ).get(CoinViewModel::class.java)
    }

    private val myAdapter by lazy {
        ImagePager(this, codeImage)
    }
    private val codeImage: ArrayList<String> = ArrayList()
    private var state: Int = 0
    private var codeBitmap: Bitmap? = null
    override fun getLayoutId(): Int = R.layout.activity_recharge

    override fun main() {
        title = getString(R.string.usdt_coin_recharge)
        setRightText(getString(R.string.coin_recharge_record)).setOnClickListener {
            RechargeRecordActivity.start(
                this,
                RechargeRecordActivity.RECHARGE
            )
        }
        copy_address_tv.setOnClickListener {
            //获取剪贴板管理器：
            val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            val mClipData = ClipData.newPlainText("Label", recharge_tv.text.toString())
            cm.setPrimaryClip(mClipData)
            showToast(resources.getString(R.string.rep_successfully))
        }
        showProgress(true)
        coinViewModel.myRechargeAddressLiveData.observe(this, Observer {
            showProgress(false)
            if (it != null) {
                codeImage.clear()
                rechargeAddress = it
                if (!TextUtils.isEmpty(it.ercAddress)) {
                    codeImage.add(it.ercAddress!!)
                }
                if (!TextUtils.isEmpty(it.trcAddress)) {
                    codeImage.add(it.trcAddress!!)
                }
                myAdapter.setdata(codeImage)
                myAdapter.notifyDataSetChanged()
                if (!TextUtils.isEmpty(it.ercAddress)) {
                    recharge_tv.text = it.ercAddress
                }
            }
        })
        coinViewModel.errorLiveData.observe(this, Observer {
            showProgress(false)
        })

        tvLeft.setOnClickListener {
            if (codeImage.size < 1) {
                return@setOnClickListener
            }
            state = 0
            tvLeft.setBackgroundResource(R.drawable.recharge_btn_selected)
            tvLeft.setTextColor(resources.getColor(R.color.chart_white))
            tvRight.setBackgroundResource(R.drawable.recharge_hollow_btn)
            tvRight.setTextColor(resources.getColor(R.color.main_theme_color))
            viewPager.currentItem = 0
            if (rechargeAddress != null) {
                if (!TextUtils.isEmpty(rechargeAddress!!.ercAddress)) {
                    recharge_tv.text = rechargeAddress!!.ercAddress
                }
            }
        }
        tvRight.setOnClickListener {
            if (codeImage.size <= 1) {
                return@setOnClickListener
            }
            state = 1
            tvRight.setBackgroundResource(R.drawable.recharge_btn_selected)
            tvRight.setTextColor(resources.getColor(R.color.chart_white))
            tvLeft.setBackgroundResource(R.drawable.recharge_hollow_btn)
            tvLeft.setTextColor(resources.getColor(R.color.main_theme_color))
            viewPager.currentItem = 1
            if (rechargeAddress != null) {
                if (!TextUtils.isEmpty(rechargeAddress!!.trcAddress)) {
                    recharge_tv.text = rechargeAddress!!.trcAddress
                }
            }
        }
        viewPager.adapter = myAdapter
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                if (position == 0) {
                    tvLeft.setBackgroundResource(R.drawable.recharge_btn_selected)
                    tvLeft.setTextColor(resources.getColor(R.color.chart_white))
                    tvRight.setBackgroundResource(R.drawable.recharge_hollow_btn)
                    tvRight.setTextColor(resources.getColor(R.color.main_theme_color))
                    if (rechargeAddress != null) {
                        if (!TextUtils.isEmpty(rechargeAddress!!.ercAddress)) {
                            recharge_tv.text = rechargeAddress!!.ercAddress
                        }
                    }
                } else if (position == 1) {
                    tvRight.setBackgroundResource(R.drawable.recharge_btn_selected)
                    tvRight.setTextColor(resources.getColor(R.color.chart_white))
                    tvLeft.setBackgroundResource(R.drawable.recharge_hollow_btn)
                    tvLeft.setTextColor(resources.getColor(R.color.main_theme_color))
                    if (rechargeAddress != null) {
                        if (!TextUtils.isEmpty(rechargeAddress!!.trcAddress)) {
                            recharge_tv.text = rechargeAddress!!.trcAddress
                        }
                    }
                }
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })

        viewPager.currentItem = 0
    }

    override fun onDestroy() {
        super.onDestroy()
        codeBitmap?.recycle()
        codeBitmap = null
    }

    class ImagePager(
         var context: Context,
         var strDrawables: ArrayList<String>
    ) : PagerAdapter() {

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val flater: LayoutInflater =
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val view = flater.inflate(R.layout.item_qrcode, null)
            var qrCodeIv = view.findViewById<ImageView>(R.id.qrCodeIv)
            val height = ScreenUtil.dip2px(context, 200f).toInt()
            var codeBitmap = QRCodeUtils.createQRCodeBitmap(strDrawables[position], height, height)
            qrCodeIv.setImageBitmap(codeBitmap)
            (container as ViewPager).addView(view, 0)
            return view
        }

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view === `object`
        }

        override fun getCount(): Int {
            return strDrawables.size
        }

        fun setdata(codeImage: ArrayList<String>) {
            this.strDrawables = codeImage
        }

    }

    override fun onResume() {
        super.onResume()
        coinViewModel.getRechargeAddress()
    }
}